package com.robot.merchant.service;

import com.robot.merchant.dao.MerchantMemberDao;
import com.robot.merchant.pojo.MerchantMember;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * member服务层
 *
 * @author lin
 */
@Service
public class MerchantMemberService {

    @Autowired
    private MerchantMemberDao merchantMemberDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<MerchantMember> findAll() {
        return merchantMemberDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<MerchantMember> findSearch(Map whereMap, int page, int size) {
        Specification<MerchantMember> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return merchantMemberDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<MerchantMember> findSearch(Map whereMap) {
        Specification<MerchantMember> specification = createSpecification(whereMap);
        return merchantMemberDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public MerchantMember findById(String id) {
        return merchantMemberDao.findById(id).orElse(null);
    }

    /**
     * 增加
     *
     * @param member
     */
    public void add(MerchantMember member) {
        //雪花分布式ID生成器
        member.setId(idWorker.nextId() + "");
        merchantMemberDao.save(member);
    }

    /**
     * 修改
     *
     * @param member
     */
    public void update(MerchantMember member) {
        merchantMemberDao.save(member);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        merchantMemberDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<MerchantMember> createSpecification(Map searchMap) {
        return new Specification<MerchantMember>() {
            @Override
            public Predicate toPredicate(Root<MerchantMember> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // id
                String id = "id";
                if (searchMap.get(id) != null && !"".equals(searchMap.get(id))) {
                    predicateList.add(cb.like(root.get(id).as(String.class), "%" + (String) searchMap.get(id) + "%"));
                }
                // 公司id
                String companyId = "companyId";
                if (searchMap.get(companyId) != null && !"".equals(searchMap.get(companyId))) {
                    predicateList.add(cb.like(root.get(companyId).as(String.class), "%" + (String) searchMap.get(companyId) + "%"));
                }
                // 代理商所在区县
                String storeId = "storeId";
                if (searchMap.get(storeId) != null && !"".equals(searchMap.get(storeId))) {
                    predicateList.add(cb.like(root.get(storeId).as(String.class), "%" + (String) searchMap.get(storeId) + "%"));
                }
                // 会员id
                String memberId = "memberId";
                if (searchMap.get(memberId) != null && !"".equals(searchMap.get(memberId))) {
                    predicateList.add(cb.like(root.get(memberId).as(String.class), "%" + (String) searchMap.get(memberId) + "%"));
                }
                // 昵称
                String nickname = "nickname";
                if (searchMap.get(nickname) != null && !"".equals(searchMap.get(nickname))) {
                    predicateList.add(cb.like(root.get(nickname).as(String.class), "%" + (String) searchMap.get(nickname) + "%"));
                }
                // 手机号
                String phone = "phone";
                if (searchMap.get(phone) != null && !"".equals(searchMap.get(phone))) {
                    predicateList.add(cb.like(root.get(phone).as(String.class), "%" + (String) searchMap.get(phone) + "%"));
                }
                // 头像
                String avatar = "avatar";
                if (searchMap.get(avatar) != null && !"".equals(searchMap.get(avatar))) {
                    predicateList.add(cb.like(root.get(avatar).as(String.class), "%" + (String) searchMap.get(avatar) + "%"));
                }
                // 推荐人
                String recommender = "recommender";
                if (searchMap.get(recommender) != null && !"".equals(searchMap.get(recommender))) {
                    predicateList.add(cb.like(root.get(recommender).as(String.class), "%" + (String) searchMap.get(recommender) + "%"));
                }
                //剔除软删除数据
                String deletedAt = "deletedAt";
                predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(LocalDateTime.class)));

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * 通过store_id查找店铺今日会员
     *
     * @param storeId
     * @param page
     * @param size
     * @return
     */
    public Page<MerchantMember> findMerchantMembersByStoreIdOrderByRegisteredAtDescToday(String storeId, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return merchantMemberDao.findMerchantMembersByStoreIdOrderByRegisteredAtDescToday(storeId, pageable);
    }

    /**
     * 通过store_id查找店铺昨日会员
     *
     * @param storeId
     * @param page
     * @param size
     * @return
     */
    public Page<MerchantMember> findMerchantMembersByStoreIdOrderByRegisteredAtDescYesterday(String storeId, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return merchantMemberDao.findMerchantMembersByStoreIdOrderByRegisteredAtDescYesterday(storeId, pageable);
    }

    /**
     * 通过store_id查找店铺最近7日会员
     *
     * @param storeId
     * @param page
     * @param size
     * @return
     */
    public Page<MerchantMember> findMerchantMembersByStoreIdOrderByRegisteredAtDescWeek(String storeId, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return merchantMemberDao.findMerchantMembersByStoreIdOrderByRegisteredAtDescWeek(storeId, pageable);
    }

    /**
     * 通过store_id查找店铺的总会员
     *
     * @param storeId
     * @param page
     * @param size
     * @return
     */
    public Page<MerchantMember> findMerchantMembersByStoreIdOrderByRegisteredAtDescTotal(String storeId, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return merchantMemberDao.findMerchantMembersByStoreIdOrderByRegisteredAtDescTotal(storeId, pageable);
    }

    /**
     * 通过storeId和memberId查找代理商名下的某一会员
     *
     * @param agentId
     * @param memberId
     * @return
     */
    public MerchantMember findByStoreIdAndMemberId(String agentId, String memberId) {
        return merchantMemberDao.findByStoreIdAndMemberId(agentId, memberId);
    }
}
